package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * guoguo
 * 后台商品管理controller
 */
@RequestMapping("/manage/product")
@Controller
public class ProductManageController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IProductService productService;
    /**
     * 商品的新增或者修改
     * @param session
     * @param product
     * @return
     */
    @RequestMapping(value = "save.do")
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product){
        //判断用户是否登录
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登录");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //处理保存商品/修改商品的逻辑
            return productService.productSaveOrUpdate(product);
        }else{
            return  ServerResponse.createByErrorMessage("你无权操作，需要管理员权限");
        }
    }

    /**
     * 更新商品的销售状态,,
     * @param session
     * @param productId
     * @param status
     * @return
     */
    @RequestMapping(value = "set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(HttpSession session,Integer productId,Integer status){
        User  user =(User)session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登录");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //更改商品的状态
            return productService.setSaleStatus(productId,status);
        }else{
            return  ServerResponse.createByErrorMessage("你无权操作，请联系管理员");
        }
    }

    /**
     * 获取商品的详情
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping(value = "detail.do")
    @ResponseBody
    public ServerResponse getProductDetail(HttpSession session,Integer productId){
        User  user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登录");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //查询商品的详情
            return productService.getProductDetail(productId);
        }else{
            return  ServerResponse.createByErrorMessage("你无权操作，请联系管理员");
        }
    }

    /**
     * 查询商品的列表
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse getList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User  user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登录");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //查询商品的详情
            return productService.getProductList(pageNum,pageSize);
        }else{
            return  ServerResponse.createByErrorMessage("你无权操作，请联系管理员");
        }
    }

    /**
     * 根据商品的名字以及商品的Id查询商品
     * @param session
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param productId
     * @return
     */
    @RequestMapping(value = "search.do")
    @ResponseBody
    public ServerResponse search(HttpSession session,
                                 @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                 String productName,
                                 Integer productId){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"需要登录");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //查询商品
            return productService.searchProduct(pageNum,pageSize,productName,productId);
        }else{
            return ServerResponse.createByErrorMessage("你无权操作，请联系管理员");
        }
    }


}
